﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
//using System.Xml.Linq;
using System.Data;
using KPIS.Utils;
using KPIS.DBM;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.MODEL;
using Telerik.Web.UI;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class MP_PositionExtra : System.Web.UI.UserControl
    {
        private object _dataItem = null;
        private IIncomeTypeBLL extBLL = new IncomeTypeBLL();
        private PositionExtraBLL posExtBLL = new PositionExtraBLL();
        private IClassBLL classBLL = new ClassBLL();
        private IPositionBLL posBLL = new PositionBLL();
        private readonly SsMng ssMng = new SsMng();

        //ReadOnly property.
        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }

        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }
        //Initializing the new variable updating ViewState data.
       protected override void OnLoad(System.EventArgs e)
       {
           base.OnLoad(e);

           try
           {
               txtAmount.Attributes.Add("onkeypress", "return isNumberKeyDot(event)");

               if (!IsUserControlPostBack)
               {
                   this.ViewState.Add("IsUserControlPostBack", true);
               }
               if (DataItem != null)
               {
                   bindClass();
                   bindExtraType();

                   if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                   {
                       lblSequence.Text = DataBinder.Eval(DataItem, "pos_ext_seq").ToString();
                       cmbExtType.SelectedValue = DataBinder.Eval(DataItem, "ext_seq").ToString();
                       cmbClass.SelectedValue = DataBinder.Eval(DataItem, "class_seq").ToString();
                       txtAmount.Text = DataBinder.Eval(DataItem, "amount", "{0:N2}").ToString();

                       this.ViewState.Add("IsUserControlPostBack", true);
                   }
               }
           } catch (Exception ee)
           {
           }
       }
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }
        private void bindClass()
        { 
            //Nullable<int> nullPositionType = string.IsNullOrEmpty(ddlPosiType.SelectedValue) ? (int?)null : Int32.Parse(ddlPosiType.SelectedValue);
            //Nullable<int> nullEmloyeeType = string.IsNullOrEmpty(ddlEmployeeType.SelectedValue) ? (int?)null : Int32.Parse(ddlEmployeeType.SelectedValue);
            /*
            Nullable<int> PosSeq = string.IsNullOrEmpty(Session["POS_SEQ"].ToString()) ? (int?)null : Int32.Parse(Session["POS_SEQ"].ToString());

            DataSet ds = classBLL.LoadClassList((int?)null, 1, PosSeq, (int?)null, (int?)null);

            if (ds.Tables.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                cmbClass.DataSource = ds;
                cmbClass.DataBind();
            }*/
        }
        private void bindExtraType()
        {
            try
            {
                int nEmpTypeSeq = string.IsNullOrEmpty(Session["EMP_TYPE_SEQ"].ToString()) ? 0 : Int32.Parse(Session["EMP_TYPE_SEQ"].ToString());

                DataSet ds = extBLL.ListIncomeType(nEmpTypeSeq, SystemInfo._ActiveRecordStatus);

                if (ds.Tables.Count > 0)
                {
                    cmbExtType.DataSource = ds.Tables[0];
                    cmbExtType.DataBind();
                }

            }catch( Exception e)
            {
                
            }
        }

        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            try
            {
                string posSeq = Session["POS_SEQ"].ToString();
				string classSeq = Convert.ToString(Session["CLASS_SEQ"]);// Convert.ToString(Session["CLASS_SPEC"]);// cmbClass.SelectedValue);
                string extType = cmbExtType.SelectedValue;
                string amountExtra = txtAmount.Text;
                int employeeId = ssMng.EmpSeqI;

                posExtBLL.Insert( Convert.ToInt32(posSeq) // don't allow null
                                 , String.IsNullOrEmpty(classSeq) ? -1 : Convert.ToInt32(classSeq)
                                 , String.IsNullOrEmpty(extType) ? -1 : Convert.ToInt32(extType)
                                 , String.IsNullOrEmpty(amountExtra) ? -1 : Double.Parse(amountExtra)
                                 , ""
                                 , employeeId);
            }
            catch(Exception ex)
            {
                
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        { 
            try
            {
                string classSeq = Convert.ToString(Session["CLASS_SEQ"]);// cmbClass.SelectedValue);
                string extType = cmbExtType.SelectedValue;
                string amountExtra = txtAmount.Text;
                string seq = lblSequence.Text;
                int employeeId = ssMng.EmpSeqI;

                posExtBLL.Update(Convert.ToInt32(seq) // don't allow null
                                 , String.IsNullOrEmpty(classSeq) ? -1 : Convert.ToInt32(classSeq)
                                 , String.IsNullOrEmpty(extType) ? -1 : Convert.ToInt32(extType)
                                 , String.IsNullOrEmpty(amountExtra) ? -1 : Double.Parse(amountExtra)
                                 , ""
                                 , employeeId);
            }
            catch (Exception ex)
            {

            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

    }
}